﻿// ==UserScript==
// @name         outlook-auto
// @namespace    https://user-web.googlecode.com/
// @version      8
// @description  auto login to outlook
// @grant        none
// @match        https://*.com/owa/*
// @match        https://*.com/dana-na/auth/url*
// @copyright    2010+, james_zerty
// ==/UserScript==

try {
    var isTop = window.top == window.self;

    if (isTop) {
        var outlookAuto = new function () {
            var me = this;

            me.load = function () {
                var url = document.location.href;

                me.log("starting");

                var checkbox = document.getElementsByName("postfixSID");
                var timeoutDiv = document.getElementById("signindiv");
                var proceedButton = document.getElementById("sn-preauth-proceed_2");
                var loginButton = document.getElementById("btnSubmit_6");
                var concurrentExceeded = document.getElementById("table_ExceededConcurrent_3");
                var passwordExpiration = document.getElementById("table_PasswordExpiration_1");
                var errMsg = document.getElementById("errMsg");

                if (errMsg != null) {
                    me.log("errMsg found");

                    if (errMsg.innerHTML.indexOf("already signed in to Outlook") > -1) {
                        me.log("errMsg found: already signed in to Outlook");
                        document.location.reload();
                    }
                }
                else if (checkbox != null && checkbox.length > 0) {
                    me.log("chk 1");
                    checkbox[0].checked = true;
                    me.log("chk 2");
                    document.getElementById("btnContinue").disabled = false;
                    me.log("chk 3");
                    window.setTimeout(function () {
                        me.log("chk 4");
                        document.getElementById("btnContinue").click();
                    }, 1000);
                }
                else if (timeoutDiv != null) {
                    me.log("timeout found");
                    timeoutDiv.parentNode.click();
                }
                else if (proceedButton != null) {
                    proceedButton.click();
                }
                else if (loginButton != null) {
                    me.log("loginButton found");

                    window.setTimeout(function () {
                        me.log("checking password_5");

                        if (document.getElementById("password_5").value != "") {
                            var msgTable = document.getElementById("table_LoginPage_5");

                            //Alert: The number of concurrent users signed in to the system has exceeded the system limit. Please inform your system administrator. You can try again in a few minutes.</td></tr></tbody></table>
                            if (msgTable) {
                                me.log("password_5 non-blank, alert found, not submitting");
                            }
                            else {
                                me.log("password_5 non-blank, submitting...");
                                loginButton.click();
                            }
                        }
                    }, 1000);
                }
                else if (concurrentExceeded != null || passwordExpiration != null) {
                    me.log("concurrentExceeded or passwordExpiration found");

                    document.getElementsByTagName("A")[0].click();
                }
                else {
                    me.log("keep alive init");

                    window.setInterval(function () {
                        me.keepAlive();
                    }, 1 * 60 * 1000);

                    //document.getElementById("txtS").onclick = function () {
                    //    me.keepAlive();
                    //};
                    //me.checkForTimeout();
                }
            };

            me.keepAlive = function () {
                try {
                    var ds = me.getDateString();
                    me.log("keep alive...");

                    var ghcnts = document.getElementsByClassName("ghcnts");

                    for (var i = 0; i < ghcnts.length; i++) {
                        ghcnts[i].innerHTML = ds.substr(0, 5) + " Reloading...";
                    }

                    document.getElementById("spnUserTileTxt").innerHTML = ds.substr(0, 5);
                    document.getElementById("checkmessages").click();

                    window.setTimeout(function () {
                        me.checkForTimeout();
                    }, 5000);
                }
                catch (ex) {
                    log("error", ex.message);
                }
            };

            me.checkForTimeout = function () {
                try {
                    //if (!document.getElementById("divNI") && !document.getElementById("divChnk")) {
                    //if (document.getElementById("#divScrollRegion")) {
                    if (document.getElementById("divList").innerHTML.length < 250) {
                        me.log("...timeout detected");

                        if (confirm("You've been logged out. Log back in?")) {
                            document.location.reload();
                        }
                    }
                    else {
                        me.log("...still logged in");
                    }
                }
                catch (ex) {
                    log("error", ex.message);
                }
            };

            me.getDateString = function () {
                var date = new Date();
                var h = date.getHours();
                var n = date.getMinutes();
                var s = date.getSeconds();

                //var d = date.getDate();
                //var m = date.getMonth() + 1;
                //var y = date.getFullYear();
                //return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
                return '' + (h <= 9 ? '0' + h : h) + ':' + (n <= 9 ? '0' + n : n) + ':' + (s <= 9 ? '0' + s : s);
            };

            me.log = function (text1, text2) {
                log(me.getDateString() + " " + text1, text2);
            };
        }

        /* --------------------------------------------------------------- */

        window.setTimeout(function () {
            try {
                outlookAuto.load();
            }
            catch (ex) {
                log("error", ex.message);
            }
        }, 100);
    }
}
catch (ex) {
    if (ex.message.indexOf("accessing a cross-origin frame") > -1) {
        log("frame", ex.message);
    }
    else {
        log("error", ex.message);
    }
}

function log(text1, text2) {
    if (text2 != null) {
        if (text1.length < 20) {
            text1 = text1 + Array(21 - text1.length).join(" ");
        }

        if (text1.length > 20) {
            text1 = text1.substr(0, 20);
        }

        text1 = text1 + ": " + text2;
    }

    if (text1.length > 100) {
        text1 = text1.substr(0, 100) + "...";
    }

    console.log("[outlook-auto] " + text1);
};

//end